﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc ;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace MShop.Core.Domain
{
    [MetadataType(typeof(Department_Validation))]
    public partial class Department:INode<Department>
    {
        public Department()
        {
            CreateTime = DateTime.Now;
        }

        public object Key
        {
            get { return this.Id; }
        }


        public string TableRowClass
        {
            get
            {
                if (this.ParentId.HasValue)
                {
                    return string.Concat("child-of-node-", this.Department2.NodeId);
                }
                else
                    return null;
            }
        }

        public string TableRowId
        {
            get
            {
                return string.Concat("node-", NodeId);
            }
        }

        public bool IsRoot
        {
            get { return !this.ParentId.HasValue; }
        }

        public int Depth
        {
            get
            {
                int i = 0;
                var p = this.Department2;
                while (p != null)
                {
                    p = p.Department2;
                    i++;
                }
                return i;
            }
        }

        public int NodeId { get; set; }



        public bool HasChild
        {
            get
            {
                return this.Department1.Count > 0;
            }
        }

       


        public Department Parent
        {
            get { return this.Department2; }
        }

        public IEnumerable<Department> ChildNodes
        {
            get
            {
                return this.Department1;
            }
        }


         int? INode.ParentId
        {
            get { return this.ParentId; }
        }
        
    }

    internal sealed class Department_Validation
    {
        [DisplayName("父级部门")]        
        public int ParentId { get; set; }

        [Required(ErrorMessage = "部门名称不能为空")]
        [DisplayName("部门名称")]
        public string DeptName { get; set; }


        [DisplayName("部门备注")]
        public string DeptMemo { get; set; }
    }
}